package com.ly.loginstarter.web;

import com.ly.loginstarter.biz.LoginBiz;
import com.ly.loginstarter.dao.LoginDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
public class LoginAction {

    @Resource
    private LoginBiz loginBiz;

    @RequestMapping("login")
    public String login(String username, String password, HttpSession session) {
        final Map<String, Object> user = loginBiz.login(username, password);
        if (user == null) {
            return "登录失败";
        } else {
            session.setAttribute("loginedUser", user);
            return "登录成功";
        }
    }

    @RequestMapping("getLoginedUser")
    public Map<String, Object> login(HttpSession session) {
        return (Map<String, Object>) session.getAttribute("loginedUser");
    }

    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "退出成功!";
    }

    @Resource
    private LoginDao dao;
    @Value("${ly.login.column-mail}")
    private String columnMail;

    // 用户忘记密码, 发送邮件
    @RequestMapping("forget")
    public String forget(String username, HttpSession session) {
        final Map<String, Object> user = dao.selectByName(username);
        if (user == null) {
            return "没有该用户!";
        } else {
            String mail = (String) user.get(columnMail);
            // 生成验证码
            String code = System.currentTimeMillis() + "";
            // 截取后四位
            code = code.substring(code.length() - 4);
            // 发送邮件
            String msg = "重置密码的验证码是: " + code;
            System.out.println(msg);
            sendCodeEmail(mail, "密码重置", msg);
            // 将验证码存入会话
            session.setAttribute("vcode", code);
            session.setAttribute("username", username);
            return "密码重置的验证码已经发送到: " + mail;
        }
    }

    @RequestMapping("resetPwd")
    public String resetPwd(String pwd1, String pwd2, String icode,
                           // 提取会话中的数据
                           @SessionAttribute String username,
                           @SessionAttribute String vcode) {
        if (icode.equals(vcode) == false) {
            return "验证码输入错误!";
        }
        if (pwd1.equals(pwd2) == false) {
            return "2次密码输入不一致!";
        }
        dao.updatePwd(username, pwd1);
        return "密码重置成功!";

    }

    private void sendCodeEmail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    @Resource
    private JavaMailSender mailSender;
    @Value("${ly.login.vcodeFrom}")
    private String from;

}
